package com.hailong.zyt.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hailong.zyt.beans.Homework;
import com.hailong.zyt.beans.HomeworkStudent;
import com.hailong.zyt.beans.Student;
import com.hailong.zyt.service.HomeworkStudentService;
import com.hailong.zyt.utils.FileUploadUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hailong
 * @since 2021-06-01
 */
@Slf4j
@Controller
public class HomeworkStudentController {

    private HomeworkStudentService homeworkStudentService;

    @Autowired
    public void setHomeworkStudentService(HomeworkStudentService homeworkStudentService) {
        this.homeworkStudentService = homeworkStudentService;
    }

    /**
     * 展示某作业学生完成情况列表
     * --homeworkList.html
     *
     **/
    @GetMapping("/teacher/homeworkList/")
        public String toHomeworkList(@RequestParam(value = "hwId") Long hwId,
                                     @RequestParam(value = "nowPage", defaultValue = "1") Integer nowPage,
                                     Model model,
                                     HttpSession session) {
        //获取完成
        IPage<HomeworkStudent> homeworks = homeworkStudentService.getStudentHomeworkByHwId(nowPage, hwId);
        //获取未完成
        List<Student> stuNotFinished = homeworkStudentService.getStudentNotFinished(hwId);
        model.addAttribute("homeworks", homeworks);
        model.addAttribute("stuNotFinished", stuNotFinished);
        model.addAttribute("hwId", hwId);
        session.setAttribute("hwNowPage", nowPage);
        return "/teacher/homework/homeworkList";
    }

    /**
    * 批改作业
    * */
    @GetMapping("/teacher/checkHomework")
    public String toCheckHomework(@RequestParam(value = "hwId") Long hwId, @RequestParam("stuId") Long stuId, Model model) {
        model.addAttribute("homework", homeworkStudentService.getOneHomework(hwId, stuId));
        return "/teacher/homework/checkHomework";
    }
    @PutMapping("/teacher/checkHomework")
    public String checkHomework(HomeworkStudent homework, HttpSession session, Model model) {
        homeworkStudentService.saveHomework(homework, model);
        Integer nowPage = (Integer) session.getAttribute("hwNowPage");
        return "redirect:/teacher/homeworkList/?hwId=" + homework.getHwId() + "&nowPage=" + nowPage;
    }

    /**
     * 下载附件
     * */
    @RequestMapping(value = "/teacher/downloadExtra", method = RequestMethod.POST)
    public void downloadFile(String fileName, HttpServletResponse response) throws IOException {
        System.out.println("test");
        homeworkStudentService.downloadFile(fileName, response);
    }


    /*-------------------------------------------------------------------------------------------------------------------------------------*/

    //编辑作业
    @GetMapping("/student/editHomework/{hwId}")
    public String toEditHomeworkPage(@PathVariable("hwId") Long hwId, Model model, HttpSession session){
        Student student = (Student) session.getAttribute("student");
        HomeworkStudent homework = homeworkStudentService.getOneHomework(hwId, student.getStuId());
        model.addAttribute("homework", homework);
        return "/student/homework/editHomework";
    }

    //提交或者修改作业
    @PostMapping("/student/doHomework")
    public String doHomework(HomeworkStudent homework, @RequestParam("file") MultipartFile file, HttpSession session) throws IOException {
        Student student = (Student) session.getAttribute("student");
        log.info("学生：{}", student);
        log.info("作业：{}", homework);
        homeworkStudentService.submitHomework(homework, student, file);
        return "redirect:/student/courses";
    }

    @GetMapping("/student/checkHomework")
    public String toStuCheckHomework(@RequestParam(value = "hwId") Long hwId, @RequestParam("stuId") Long stuId, Model model) {
        HomeworkStudent homework = homeworkStudentService.getOneHomework(hwId, stuId);
        model.addAttribute("homework", homework);
        model.addAttribute("fileName", FileUploadUtil.getFileNameFromPath(homework.getHwsFileName()));
        return "/student/homework/checkHomework";
    }



}

